<template>
	<view class="container">
		<view class="search-param">
			<uni-search-bar placeholder="搜索客户地址" @confirm="search" @cancel="show"
				v-model="goodsDetailedAddress"></uni-search-bar>
		</view>

		<view class="search-results-container">
			<view v-for="(ecCustAddress, index) in addressList" :key="index">
				<uni-row>
					<uni-col :span="14">
						<view @tap="returnOrderApplysPage(ecCustAddress)">
							<view class="search-result">
								<uni-row>
									<uni-col :span="10">
										<view>
											<text>{{ ecCustAddress.contactsName }}</text>
										</view>
									</uni-col>
									<uni-col :span="14">
										<view>
											<text>{{ ecCustAddress.contactsPhone }}</text>
										</view>
									</uni-col>
								</uni-row>
							</view>
							<view class="search-result-address">
								<text>地址：{{ ecCustAddress.goodsDetailedAddress }}</text>
							</view>
						</view>
					</uni-col>
					<uni-col :span="10">
						<view class="search-result-address">
						</view>
						<view class="search-result-address">
							<button class="delete-button" @tap="goToDetailChangePage(ecCustAddress.pkid)">编辑</button>
							<button class="delete-button" @click="handleDelete(ecCustAddress)">删除</button>
						</view>
					</uni-col>
				</uni-row>
			</view>
		</view>

		<view style="position: fixed; bottom: 0px; width: 100%;">
			<uni-row>
				<uni-col :span="12">
					<view>
						<button @click="goToHome">返回首页</button>
					</view>
				</uni-col>
				<uni-col :span="12">
					<view>
						<button @tap="goToDetailAddPage()">新增客户地址</button>
					</view>
				</uni-col>
			</uni-row>
		</view>
	</view>
</template>

<script>
	import {
		listAddress,
		listUniAddress,
		delAddress,
	} from '@/api/tms/address'

	export default {
		mounted() {
			const custName = this.$route.query.weName;
			const custNo = this.$route.query.weKunnr;
			if (custName) {
				this.custName = custName;
			}
			if (custNo) {
				this.custNo = custNo;
			}
			
			//售达、送达方数据
			const agKunnr = this.$route.query.agKunnr;
			const agName = this.$route.query.agName;
			const weKunnr = this.$route.query.weKunnr;
			const weName = this.$route.query.weName;
			if (agKunnr) {
				this.agKunnr = agKunnr;
			}
			if (agName) {
				this.agName = agName;
			}
			if (weKunnr) {
				this.weKunnr = weKunnr;
			}
			if (weName) {
				this.weName = weName;
			}
			this.openAddressDetails();
		},
		data() {
			return {
				addressList: [],
				custName: "",
				goodsDetailedAddress: "",
				ecCustAddress: {
					pkid: "",
					custName: "",
					contactsName: "",
					contactsPhone: "",
					goodsDetailedAddress: "",
					addressPath: "",
				}
			};
		},
		methods: {
			goToHome() {
				uni.switchTab({
					url: '/pages/work/index',
				});
			},
			openAddressDetails() {
				listUniAddress({
					"custNo": this.custNo,
					"pageNum": 1,
					"pageSize": 200,
				}).then(response => {
					this.addressList = response.rows;
				});
			},
			show() {
				listUniAddress({
					"custNo": this.custNo,
					"pageNum": 1,
					"pageSize": 200,
				}).then(response => {
					this.addressList = response.rows;
				});
			},
			search() {
				listUniAddress({
					"custNo": this.custNo,
					"goodsDetailedAddress": this.goodsDetailedAddress,
					"pageNum": 1,
					"pageSize": 200,
				}).then(response => {
					this.addressList = response.rows;
					if(this.addressList.length === 0){
						listUniAddress({
							"custNo": this.agKunnr,
							"goodsDetailedAddress": this.goodsDetailedAddress,
							"pageNum": 1,
							"pageSize": 200,
						}).then(response => {
							this.addressList = response.rows;
						});
					}
				});
			},
			goToDetailChangePage(pkid) {
				uni.navigateTo({
					url: '/pages/ec/orderAddressChange/index?pkid=' + pkid + '&agKunnr=' + this.agKunnr + "&agName=" + this.agName+ "&weKunnr=" + this.weKunnr+ "&weName=" + this.weName,
				});
			},
			goToDetailAddPage() {
				uni.navigateTo({
					url: '/pages/ec/orderAddressDetailAdd/index?agKunnr=' + this.agKunnr + "&agName=" + this.agName+ "&weKunnr=" + this.weKunnr+ "&weName=" + this.weName,
				});
			},
			returnOrderApplysPage(ecCustAddress) {
				// 关闭当前页面  
				uni.navigateBack({
					delta: 1,
				});
		
				uni.setStorageSync('ecCustAddress', {
					custName: ecCustAddress.custName,
					contactsName: ecCustAddress.contactsName,
					contactsPhone: ecCustAddress.contactsPhone,
					goodsDetailedAddress: ecCustAddress.goodsDetailedAddress,
					addressPath: ecCustAddress.addressPath,
					addressPathId: ecCustAddress.addressPathId,
					provinceCode: ecCustAddress.provinceCode,
					cityCode: ecCustAddress.cityCode,
					countyCode: ecCustAddress.countyCode,
					streetCode: ecCustAddress.streetCode,
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const pkids = row.pkid || this.ids;
				this.$modal.confirm('是否确认删除客户地址管理编号为"' + pkids + '"的数据项？').then(function() {
					return delAddress(pkids);
				}).then(() => {
					this.openAddressDetails(this.custName);
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
		},
	};
</script>

<style>
	.container {
		dispaly: flex;
		flex-direction: row;
		flex: auto;
		background-color: #fff
	}

	.page-title {
		background: #3e7baf;
		color: #fff;
		text-align: center;
		font-size: 1.3rem;
		font-weight: "300";
	}

	.search-results-container {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 200%;
		padding: 3px;
		background-color: #fff;
		margin-bottom: 120px;
		padding-bottom: 120px;
	}

	.search-result {
		flex: 1;
		margin-bottom: 10px;
		background-color: #fff;
		padding: 7px;
		border-bottom: none;
		font-weight: bold;
	}

	.search-result-address {
		font-size: 12px;
		flex: 1;
		margin-bottom: 10px;
		background-color: #fff;
		padding: 7px;
		display: flex;
		align-items: center;
	}

	.delete-button {
		font-size: 12px;
		border: 1px solid orange;
		border-radius: 4px;
		color: orange;
		margin-right: 0px;
	}

	.search-param {
		background-color: #fff;
		text-align: center;
		/* 将内容设置为居中 */
	}
</style>